package com.ddm.controller.vx;

import com.ddm.constant.MessageConstant;
import com.ddm.context.BaseContext;
import com.ddm.exception.DataErrorException;
import com.ddm.result.Result;
import com.ddm.service.vxService.ScoreService;
import com.ddm.vo.vxVo.ScoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("UserScoreController")
@RequestMapping("/user/score")
@Slf4j
@Api(tags = "用户成绩相关接口")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @GetMapping("/my-exams")
    @ApiOperation("获取我的考试历史记录")
    public Result<List<ScoreVO>> getMyExamHistory() {
        log.info("获取我的考试历史记录");
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        return scoreService.getUserScore(currentUserId);
    }

    @GetMapping("/exam/{examId}")
    @ApiOperation("获取某次考试的详细结果")
    public Result<ScoreVO> getExamResult(@PathVariable Long examId) {
        log.info("获取考试详细结果，考试ID: {}", examId);
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        if (examId == null) {
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return scoreService.getUserExamResult(currentUserId, examId);
    }
} 